
package com.sun.showcase.biz.mall.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.ClassifyDao;
import com.sun.showcase.biz.mall.service.ClassifyService;
import com.sun.showcase.client.domain.mall.Classify;
import com.sun.showcase.client.query.mall.ClassifyQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
@Service("classifyService")
@Transactional
public class ClassifyServiceImpl implements ClassifyService{
	@Autowired
	private ClassifyDao classifyDao;
	
	public void setClassifyDao(ClassifyDao dao) {
		this.classifyDao = dao;
	}

	public DataGrid datagrid(ClassifyQuery classifyQuery) {
		DataGrid j = new DataGrid();
		Pager<Classify> pager  = find(classifyQuery);
		List<ClassifyQuery> classifyQuerys = new ArrayList<ClassifyQuery>();
		if (pager.getRecords() != null && pager.getRecords().size() > 0) {
			ClassifyQuery q = new ClassifyQuery();
			q.setActiveFlag("1");
			for (Classify tb : pager.getRecords()) {
				ClassifyQuery b = new ClassifyQuery();
				BeanUtils.copyProperties(tb, b);
				classifyQuerys.add(b);
				/*if("0".equals(classifyQuery.getQueryLevel())) {
					q.setParentId(b.getId());
					b.setChildren(this.listAll(q));
				}*/
				q.setParentId(b.getId());
				Long count = this.classifyDao.count(q);
				if(count > 0) {
					b.setHasChildren(true);
				}
			}
		}
		j.setRows(classifyQuerys);
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<ClassifyQuery> getQuerysFromEntitys(List<Classify> classifys) {
		List<ClassifyQuery> classifyQuerys = new ArrayList<ClassifyQuery>();
		if (classifys != null && classifys.size() > 0) {
			for (Classify tb : classifys) {
				ClassifyQuery b = new ClassifyQuery();
				BeanUtils.copyProperties(tb, b);
				classifyQuerys.add(b);
			}
		}
		return classifyQuerys;
	}

	private Pager<Classify> find(ClassifyQuery classifyQuery) {
		Page<Classify> page = PageHelper.startPage(classifyQuery.getPage().intValue(), classifyQuery.getRows().intValue());
		List<Classify> list = classifyDao.findList(classifyQuery);
		Pager<Classify> pager = new Pager<Classify>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Classify add(ClassifyQuery classifyQuery) {
		Classify t = new Classify();
		BeanUtils.copyProperties(classifyQuery, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		classifyDao.save(t);
		classifyQuery.setId(t.getId());
		return t;
		
	}

	public void update(ClassifyQuery classifyQuery) {
		Classify t = classifyDao.getById(classifyQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(classifyQuery, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	classifyDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(ClassifyQuery classifyQuery) {
	    classifyDao.updatePart(classifyQuery);
	}

	public void delete(java.lang.String[] ids) {
		classifyDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.String[] ids) {
		classifyDao.deletePt(ids);
	}

	public Classify get(ClassifyQuery classifyQuery) {
		return classifyDao.getById(classifyQuery.getId());
	}

	public Classify get(String id) {
		return classifyDao.getById(id);
	}

	
	public List<ClassifyQuery> listAll(ClassifyQuery classifyQuery) {
	    List<Classify> list = classifyDao.findList(classifyQuery);
		List<ClassifyQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public List<ClassifyQuery> list4App(String parentId) {
		ClassifyQuery q1 = new ClassifyQuery();
		q1.setOrderColum("order_num");
		q1.setOrderRule("asc");
		if(StringUtils.isEmpty(parentId)) {
			q1.setQueryLevel("0");
		}else {
			q1.setParentId(parentId);
		}
		List<ClassifyQuery> list = this.listAll(q1);
		for(ClassifyQuery c : list) {
			ClassifyQuery q2 = new ClassifyQuery();
			q2.setOrderColum("order_num");
			q2.setOrderRule("asc");
			q2.setParentId(c.getId());
			List<ClassifyQuery> list2 = this.listAll(q2);
			for(ClassifyQuery c2 : list2) {
				ClassifyQuery q3 = new ClassifyQuery();
				q3.setParentId(c2.getId());
				q3.setOrderColum("order_num");
				q3.setOrderRule("asc");
				c2.setChildren(this.listAll(q3));
			}
			c.setChildren(list2);
		}
		return list;
	}
	
	
}
